package com.moyq5.permit.center.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.PageData;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult.Status;
import com.moyq5.permit.center.bean.embed.AgentRoleId;
import com.moyq5.permit.center.bean.entity.Agent;
import com.moyq5.permit.center.bean.entity.AgentRole;
import com.moyq5.permit.center.bean.search.AgentRole4Search;
import com.moyq5.permit.center.feign.client.AgentRoleFeign;
import com.moyq5.permit.center.web.jackson.filter.AgentRoleFilter;

/**
 * 代理商角色
 * @author Moyq5
 * @date 2019年7月25日
 */
@RestController
@RequestMapping("/agentRole")
public class AgentRoleController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(AgentRoleController.class);
	
	@Autowired
	private AgentRoleFeign agentRoleFeign;
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param agentRole4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page/role", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String pageWithRole(AgentRole4Search agentRole4s, PageData pd) throws JsonProcessingException {
		log.debug("查询代理角色信息>>>pageWithRole(AgentRole4Search, PageData)");
		if (null != getLoginAgentId()) {
			if (null == agentRole4s.getAgent()) {
				agentRole4s.setAgent(new Agent());
			}
			agentRole4s.getAgent().setAgentId(getLoginAgentId());;
		}
		return toJson(agentRoleFeign.pageWithRole(new Body<AgentRole4Search, PageData>(agentRole4s, pd)), AgentRoleFilter.getWithRole());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param agentRole4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page/agentAndRole", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String pageWithAgentAndRole(AgentRole4Search agentRole4s, PageData pd) throws JsonProcessingException {
		log.debug("查询代理角色信息>>>pageWithAgentAndRole(AgentRole4Search, PageData)");
		if (null != getLoginAgentId()) {
			if (null == agentRole4s.getAgent()) {
				agentRole4s.setAgent(new Agent());
			}
			agentRole4s.getAgent().setAgentId(getLoginAgentId());;
		}
		return toJson(agentRoleFeign.pageWithAgentAndRole(new Body<AgentRole4Search, PageData>(agentRole4s, pd)), AgentRoleFilter.getWithAgentAndRole());
	}
	
	@PutMapping("/add")
	@ResponseBody
	public ObjectResult<AgentRoleId> add(@RequestBody AgentRole agentRole) {
		log.debug("添加代理角色信息>>>add(AgentRole)");
		if (null != getLoginAgentId()) {
			agentRole.getId().setAgentId(getLoginAgentId());
		}
		ObjectResult<AgentRole> objRst = agentRoleFeign.one(agentRole.getId());
		if (objRst.getStatus() == Status.OK && null != objRst.getData()) {
			ObjectResult<AgentRoleId> idRst = new ObjectResult<AgentRoleId>();
			idRst.setOk();
			idRst.setData(agentRole.getId());
			return idRst;
		}
		return agentRoleFeign.create(agentRole);
	}
	
	@PatchMapping("/edit")
	@ResponseBody
	public StatusResult edit(@RequestBody @Validated AgentRole agentRole) {
		log.debug("更新代理角色信息>>>edit(AgentRole)");
		if (null != getLoginAgentId()) {
			agentRole.getId().setAgentId(getLoginAgentId());
		}
		return agentRoleFeign.update(agentRole);
	}
	
	@DeleteMapping("/del")
	@ResponseBody
	public StatusResult del(@RequestBody AgentRoleId agentRoleId) {
		log.debug("删除代理角色信息>>>del(AgentRoleId)");
		if (null != getLoginAgentId()) {
			agentRoleId.setAgentId(getLoginAgentId());
		}
		return agentRoleFeign.remove(agentRoleId);
	}
	
}
